package base.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import base.utlis.MD5;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entity.User;

public  class BaseAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int PAGE_SIZE = 15;
	protected int currentPage;
	protected int id;
	protected String ids;
	protected String errorMsg;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
		if (ids != null && ids.length() > 1) {
			this.ids = ids.substring(0, ids.length() - 1);
		}
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public int getCurrentPage() {
		if (currentPage < 1) {
			currentPage = 1;
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		if (currentPage < 1) {
			currentPage = 1;
		}
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	protected HttpSession getHttpSession() {
		return getHttpSession();
	}

	protected Map<String ,Object> getSession() {
		return   ActionContext.getContext().getSession();
	}

	protected void put(String key, Object value) {
		ActionContext.getContext().put(key, value);
	}

	protected String getParameter(String key) {
		return ServletActionContext.getRequest().getParameter(key);
	}

	protected User getLoginUser() {
		return (User) ServletActionContext.getRequest().getSession()
				.getAttribute(Globals.LOGIN_USER_KEY);
	}

/*	protected Department getLoginUserDepartment() {
		return (Department) ServletActionContext.getRequest().getSession()
				.getAttribute(Globals.LOGIN_USER_DEPARTMENT_KEY);
	}

	protected Position getLoginUserPosition() {
		return (Position) ServletActionContext.getRequest().getSession()
				.getAttribute(Globals.LOGIN_USER_POSITION_KEY);
	}*/

	public Map<String, Object> fitParam(String name, Object value) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put(name, value);
		return parameters;
	}

	public Map<String, Object> getMap() {
		return new HashMap<String, Object>();
	}

	public String getRealPath(String path) {
		return ServletActionContext.getRequest().getSession()
				.getServletContext().getRealPath(path);
	}

	public boolean isLogin() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Globals.LOGIN_USER_KEY) != null) {
			return true;
		} else {
			return false;
		}
	}

	public PrintWriter getWriter() {
		try {
			return ServletActionContext.getResponse().getWriter();
		} catch (IOException e) {
			return null;
		}
	}

	public void write(String str) {
		try {
			ServletActionContext.getResponse().getWriter().write(str);
		} catch (IOException e) {
		}
	}
	public String getMD5(String str) {
		MD5 md5=new MD5();
		return md5.getMD5ofStr(str);
	}
	
}
